<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');
class sialpacadbModelEditRow extends JModel {
    function __construct() {
		parent::__construct();
    }

   function getTableData($tablename)
    {
    	$db 	= &JFactory::getDBO();
    	$query	= 'SELECT * FROM '.$tablename;
		$db->setQuery($query);
		$items = $db->loadRowList();
		return $items;
    }
	function storepet($data)
    {
		$setpart = ' SET ';
		$whrpart = ' WHERE ';
		for($index = 1; $index <= $data['colcount']; $index++){
			$colname = $data['colname_'.$index];
			$setpart .= $colname.' = \''.$data[$colname].'\'';
			if(strlen(trim($data['colvalue_'.$index])) > 0){
				$whrpart .= $colname.' = \''.$data['colvalue_'.$index].'\'';
				if($index < $data['colcount']){
					$whrpart .= ' AND ';
				}
			}
			if($index < $data['colcount']){
				$setpart .= ', ';
			}
		}
       //error_log(print_r($data,true),3,'store.log');
		if($data["pagemode"] == 'NEW'){
			$query = 'INSERT INTO '.$data['tablename'].$setpart;
		}
		else{
			$query = 'UPDATE '.$data['tablename'].$setpart.$whrpart;
		}
		$db 	= &JFactory::getDBO();
		$db->setQuery($query);
		//error_log(print_r($query,true),3,'store.log');
		if($db->query()){
			return true;
		}
		else{
			$this->setError($db->getError());
			return false;
		}
	}
	function storepetax($data)
    {
		$setpart = ' SET ';
		$whrpart = ' WHERE ';
		for($index = 1; $index <= $data['colcount']; $index++){
			$colname = $data['colname_'.$index];
			$setpart .= $colname.' = \''.$data[$colname].'\'';
			if(strlen(trim($data['colvalue_'.$index])) > 0){
				$whrpart .= $colname.' = \''.$data['colvalue_'.$index].'\'';
				if($index < $data['colcount']){
					$whrpart .= ' AND ';
				}
			}
			if($index < $data['colcount']){
				$setpart .= ', ';
			}
		}
       //error_log(print_r($data,true),3,'store.log');
		$query = 'INSERT INTO '.$data['tablename'].$setpart;
		$db 	= &JFactory::getDBO();
		$db->setQuery($query);
		//error_log(print_r($query,true),3,'store.log');
		if($db->query()){
			$session =& JFactory::getSession(); $session->set('insertid', $db->insertid());
			return true;
		}
		else{
			$this->setError($db->getError());
			return false;
		}
	}
    function store($data)
    {
		$setpart = ' SET ';
		$whrpart = ' WHERE ';
		for($index = 1; $index <= $data['colcount']; $index++){
			$colname = $data['colname_'.$index];
			$setpart .= $colname.' = \''.$data[$colname].'\'';
			//if(strlen(trim($data['colvalue_'.$index])) > 0){
				$whrpart .= $colname.' = \''.$data['colvalue_'.$index].'\'';
				if($index < $data['colcount']){
					$whrpart .= ' AND ';
				}
			//}
			if($index < $data['colcount']){
				$setpart .= ', ';
			}
		}
		if($data["pagemode"] == 'NEW'){
			$query = 'INSERT INTO '.$data['tablename'].$setpart;
		}
		else{
			$query = 'UPDATE '.$data['tablename'].$setpart.$whrpart;
		}

		$db 	= &JFactory::getDBO();
		$db->setQuery($query);
				//error_log(print_r($query,true),3,"del.log");
		if($db->query()){
			return true;
		}
		else{
			$this->setError($db->getError());
			//error_log(print_r($db,true),3,"del.log");
			return false;
		}
    }
   	function delete($data)
    {
		$whrpart = ' WHERE ';
		for($index = 1; $index <= $data['colcount']; $index++){
			$colname = $data['colname_'.$index];
			$fldname = $data[$colname.'_'.$data['rowid']];
			//if(strlen(trim($fldname)) > 0){
				$whrpart .= $colname.' = \''.$fldname.'\'';
				if($index < $data['colcount']){
					$whrpart .= ' AND ';
				}
			//}
		}
		$query = 'DELETE FROM '.$data['tablename'].$whrpart;
		$db 	= &JFactory::getDBO();
		$db->setQuery($query);
		if($db->query()){
			//error_log(print_r($db,true),3,"del.log");
			return true;
		}
		else{
			$this->setError($db->getError());
			//error_log(print_r($db,true),3,"del.log");
			return false;
		}
    }
}
?>
